package com.adaxi.adc.business.vehicletrip.controller;

import com.adaxi.adc.business.vehicletrip.mapper.VehicleTripMapper;
import com.adaxi.adc.business.vehicletrip.pojo.VehicleTrip;
import com.adaxi.adc.business.vehicletrip.service.VehicleTripService;
import com.adaxi.adc.business.vehicletrip.vo.MyTripVo;
import com.adaxi.adc.business.vehicletrip.vo.VehicleTripVo;
import com.adaxi.adc.common.common.CommonResult;
import com.adaxi.adc.common.utils.UuidUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车辆行程控制器
 */
@RestController
@RequestMapping("/vehicleTrip")
public class VehicleTripController {

    final VehicleTripService vehicleTripService;

    final VehicleTripMapper mapper;

    public VehicleTripController(VehicleTripService vehicleTripService, VehicleTripMapper mapper) {
        this.vehicleTripService = vehicleTripService;
        this.mapper = mapper;
    }

    /**
     * 新增车辆行程。
     * @param vo 车辆行程信息。
     * @return CommonResult 表示插入操作成功或失败的结果。
     */
    @PostMapping("/deliver")
    public CommonResult insert(@RequestBody VehicleTripVo vo) {
        vo.setId(UuidUtil.get32UUID());
        vehicleTripService.insert(vo);
        return CommonResult.SUCCESS("订单发布成功");
    }

    /**
     * 根据ID查询车辆行程。
     * @param id 车辆行程的ID。
     * @return CommonResult 返回查询到的车辆行程或查询失败的消息。
     */
    @GetMapping("/vehicleTripById/{id}")
    public CommonResult<VehicleTrip> vehicleTripById(@PathVariable String id) {
        VehicleTrip vehicleTrip = vehicleTripService.getById(id);
        return CommonResult.SUCCESS("查询成功", vehicleTrip);
    }

    /**
     * 查询所有车辆行程。
     * @return CommonResult 返回所有车辆行程列表或查询失败的消息。
     */
    @GetMapping("/vehicleTripGetAll")
    public CommonResult<List<VehicleTrip>> vehicleTripGetAll() {
        List<VehicleTrip> list = vehicleTripService.list();
        return CommonResult.SUCCESS("查询成功", list);
    }

    /**
     * 减少车辆行程的乘客数。
     * @param id          车辆行程的ID。
     * @param numToReduce 需要减少的乘客数量。
     * @return CommonResult 表示减少操作成功或失败的结果。
     */
    @PutMapping("/reduction/{id}/{numToReduce}")
    public CommonResult reduction(@PathVariable String id, @PathVariable Integer numToReduce) {
        // 1. 查询当前的乘客数
        VehicleTrip currentTrip = mapper.selectById(id);
        if (currentTrip == null) {
            return CommonResult.FAILURE("未找到对应的行程");
        }

        int currentNumPassengers = currentTrip.getNum_passengers();
        if (currentNumPassengers < numToReduce) {
            return CommonResult.FAILURE("退票人数超过当前乘客数");
        }

        // 2. 从当前乘客数中减去退票的人数
        int newNumPassengers = currentNumPassengers - numToReduce;

        // 3. 更新乘客数
        VehicleTrip vehicleTripToUpdate = new VehicleTrip();
        vehicleTripToUpdate.setNum_passengers(newNumPassengers);

        UpdateWrapper<VehicleTrip> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);

        int update = mapper.update(vehicleTripToUpdate, updateWrapper);
        if (update >= 1) {
            return CommonResult.SUCCESS("退票成功，剩余" + newNumPassengers + "人");
        }

        return CommonResult.FAILURE("更新失败");
    }

    /**
     * 根据司机id查询我的行程信息   
     * @param id
     * @return
     */
    @GetMapping("/getMyTrip/{id}")
    public CommonResult<List<MyTripVo>> getMyTrip(@PathVariable Long id){
        List<MyTripVo> list = vehicleTripService.getMyTrip(id);
        return CommonResult.SUCCESS("查询成功",list);
    }


    /**
     * 根据行程id修改status状态
     * @param id
     * @param status
     */
    @PutMapping("/updateStatus/{id}/{status}")
    public CommonResult updateStatusById(@PathVariable String id,@PathVariable String status) {
        vehicleTripService.updateStatusById(id,status);
        return CommonResult.SUCCESS("修改成功");
    }


    @DeleteMapping("/cancelTrip/{id}")
    public CommonResult cancelTrip(@PathVariable String id){
        vehicleTripService.cancelTrip(id);
        return CommonResult.SUCCESS("取消行程成功");
    }
}


